/**
 * 
 */
package com.qihang.core.dao.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.qihang.common.PagerModel;
import com.qihang.core.dao.UserDao;
import com.qihang.core.domain.UserEntity;

/**
 * @author limn
 * 
 */
@Repository("userDao")
public class UserDaoImpl implements UserDao {

	@Resource(name="sessionFactory")
	protected SessionFactory sessionFactory;

	public Session getSession() {
		return sessionFactory.getCurrentSession();
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public boolean isExist(UserEntity paramUser) {
		String hql = "select count(1) from UserEntity where username =? and password = ?";
		Query query = this.getSession().createQuery(hql);
		query.setString(0, paramUser.getUsername());
		query.setString(1, paramUser.getPassword());
		Long count = (Long) query.uniqueResult();
		if (count >= 1) {
			return true;
		}
		return false;
	}

	@Override
	public void delUser(UserEntity paramUser) {
		this.getSession().delete(paramUser);
		// String hql = "delete UserEntity as p where p.id=?";
		// Query query = session.createQuery(hql);
		// query.setInteger(0, paramUser.getId());
		// int succ = query.executeUpdate();
		// return 0 == succ ? false : true;
	}

	@Override
	public void updateUser(UserEntity paramUser) {
		this.getSession().update(paramUser);
	}

	@Override
	public PagerModel getUserList(UserEntity paramUser) {
		String hql = "from UserEntity";
		Query query = this.getSession().createQuery(hql);
		PagerModel pages = new PagerModel(paramUser.getPageNo(), 10, query);
		return pages;
	}

	@Override
	public UserEntity getUser(UserEntity paramUser) {
		UserEntity userEntity = (UserEntity) this.getSession().get(
				UserEntity.class, paramUser.getId());
		return userEntity;
	}

	@Override
	public boolean addUser(UserEntity paramUser) {
		Serializable succ = this.getSession().save(paramUser);
		return null == succ ? false : true;
	}

}
